﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsTemplateFile
    {
        private readonly KeRui.DAL.CmsTemplateFile dal = new KeRui.DAL.CmsTemplateFile();

        public int Add(KeRui.Model.CmsTemplateFile model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsTemplateFile> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsTemplateFile> list = new List<KeRui.Model.CmsTemplateFile>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsTemplateFile item = new KeRui.Model.CmsTemplateFile();
                    if (dt.Rows[i]["FileID"].ToString() != "")
                    {
                        item.FileID = int.Parse(dt.Rows[i]["FileID"].ToString());
                    }
                    if (dt.Rows[i]["TemplateID"].ToString() != "")
                    {
                        item.TemplateID = int.Parse(dt.Rows[i]["TemplateID"].ToString());
                    }
                    item.FileName = dt.Rows[i]["FileName"].ToString();
                    item.FilePath = dt.Rows[i]["FilePath"].ToString();
                    item.FileContent = dt.Rows[i]["FileContent"].ToString();
                    if (dt.Rows[i]["FileType"].ToString() != "")
                    {
                        item.FileType = int.Parse(dt.Rows[i]["FileType"].ToString());
                    }
                    if (dt.Rows[i]["FileUpdateInterval"].ToString() != "")
                    {
                        item.FileUpdateInterval = int.Parse(dt.Rows[i]["FileUpdateInterval"].ToString());
                    }
                    if (dt.Rows[i]["FileUpdateTime"].ToString() != "")
                    {
                        item.FileUpdateTime = DateTime.Parse(dt.Rows[i]["FileUpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["UsingNum"].ToString() != "")
                    {
                        item.UsingNum = int.Parse(dt.Rows[i]["UsingNum"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["IsDefault"].ToString() != "")
                    {
                        item.IsDefault = int.Parse(dt.Rows[i]["IsDefault"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int FileID)
        {
            this.dal.Delete(FileID);
        }

        public List<KeRui.Model.CmsTemplateFile> GetList(int FileType)
        {
            string strWhere = "";
            if (FileType >= 0)
            {
                strWhere = " FileType=" + FileType;
            }
            string strOrder = "FileName asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsTemplateFile", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsTemplateFile> GetList(string strWhere)
        {
            string strOrder = "FileName asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsTemplateFile", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsTemplateFile> GetList(int TemplateID, int FileType)
        {
            string strWhere = "TemplateID=" + TemplateID;
            if (FileType >= 0)
            {
                strWhere = strWhere + "and FileType=" + FileType;
            }
            string strOrder = "FileName asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsTemplateFile", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsTemplateFile> GetList(int TemplateID, int FileType, string FileName)
        {
            string strWhere = "TemplateID=" + TemplateID;
            if (FileType >= 0)
            {
                strWhere = strWhere + "and FileType=" + FileType;
            }
            if (FileName != "")
            {
                strWhere = strWhere + "and FileName like '%" + Utils.SqlStringFormat(FileName, 2) + "%'";
            }
            string strOrder = "FileName asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsTemplateFile", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsTemplateFile> GetListByCache(int TemplateID, int FileType, string FileName)
        {
            string cacheKey = string.Concat(new object[] { "CmsTemplateFileList-", TemplateID, "-", FileType, "-", FileName });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(TemplateID, FileType, FileName);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsTemplateFile>) cache;
        }

        public KeRui.Model.CmsTemplateFile GetModel(int FileID)
        {
            return this.dal.GetModel(FileID);
        }

        public KeRui.Model.CmsTemplateFile GetModelByCache(int FileID)
        {
            string cacheKey = "CmsTemplateFileModel-" + FileID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(FileID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsTemplateFile) cache;
        }

        public DataSet GetSelect(int TemplateID, int FileType)
        {
            string strWhere = "";
            strWhere = string.Concat(new object[] { "TemplateID=", TemplateID, " and (FileType=", FileType, " or FileType=6)" });
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsTemplateFile", strWhere, "  IsTop Desc", " FilePath,FileName");
        }

        public DataSet GetSelect1(int TemplateID, int FileType)
        {
            string strWhere = "";
            strWhere = string.Concat(new object[] { "TemplateID=", TemplateID, " and FileType=", FileType, " " });
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsTemplateFile", strWhere, "  IsTop Desc", " FilePath,FileName");
        }

        public List<KeRui.Model.CmsTemplateFile> GetSelectList(int TemplateID, int FileType)
        {
            return this.DataTableToList(this.GetSelect(TemplateID, FileType).Tables[0]);
        }

        public void Reduce(KeRui.Model.CmsTemplateFile model)
        {
            this.dal.Reduce(model);
        }

        public void Update(KeRui.Model.CmsTemplateFile model)
        {
            this.dal.Update(model);
        }
    }
}

